<?php
namespace addons\unishop\controller;

use app\common\controller\Api;
use think\Cache;
use think\Db;
use think\Lang;

/**
 * 基础类
 * @ApiInternal
 */
class Service extends Api{
    /**
     * 无需鉴权的方法,但需要登录
     * @var array
     */
    protected $noNeedRight = ['*'];


    /**
    * 提交订单
     */
    public function submits(){

        $user=$this->auth->getUser();

        $log['backgood_id']=$this->request->param('backgood_id');
        $log['desc']=$this->request->param('desc');
        $log['images']=$this->request->param('images');
        $log['name']=$this->request->param('name');
        $log['mobile']=$this->request->param('mobile');
        $log['address']=$this->request->param('address');
        $log['imel']=$this->request->param('imel');

        foreach ($log as $key=>$v){

            if (!$v){
                 $this->error('请填写完整信息'.$key);
            }


        }
        $log['user_id']=$user['id'];


        $set=Db::name('aa_service_set')->where(['id'=>1])->find();


        $log['money']=$set['money'];
        $log['time']=time();

        $id=Db::name('aa_service')->insertGetId($log);

        $this->success('提交订单',['id'=>$id]);

    }

    /**
     * 微信支付
     */
    public function wxpay(){

        $appids=Db::name('unishop_config')->where(['name'=>'app_id'])->find();
        $mch_ids=Db::name('unishop_config')->where(['name'=>'mch_id'])->find();
        $keys=Db::name('unishop_config')->where(['name'=>'key'])->find();
        $userinfo=$this->auth->getUserinfo();

        $user=Db::name('user')->where(['id'=>$userinfo['id']])->find();

        $oid=$this->request->param('id');

        $order=Db::name('aa_service')->where(['id'=>$oid])->find();


        $appid=$appids['value'];
        $mch_id=$mch_ids['value'];
        $nonce_str=createNoncestr();
        $zfmoney=$order['money'];
        $openid=$user['openid'];
        $key=$keys['value']; //32位字符
        $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';

        $in['user_id']=$user['id'];
        $in['order_id']=$oid;
        $in['order']=$nonce_str;

        Db::name('aa_servicepay')->data($in)->insert();

        $parameters = array(
            'appid' => $appid, //服务商ID
            'mch_id' => $mch_id, //商户号
            'nonce_str' => createNoncestr(), //随机字符串
            'body' => '充值',
            'out_trade_no' => $nonce_str,
            'total_fee' => $zfmoney * 100, //分
            'spbill_create_ip' => $_SERVER['REMOTE_ADDR'], //终端IP
            'notify_url' => 'https://' . $_SERVER['HTTP_HOST'] . '/api/pay/payback',  //回调
            'openid' =>$openid, //用户id
            'trade_type' => 'JSAPI'//交易类型
        );



        //统一下单签名
        $parameters['sign'] = getSign($parameters, $key);
        $xmlData = arrayToXml($parameters);
        $postxml=postXmlCurl($xmlData, $url, 60);
        $return = xmlToArray($postxml);

//        print_r($return);

        $parameter = array(
            'appId' => $appid, //小程序ID
            'timeStamp' => '' . time() . '', //时间戳
            'nonceStr' => $nonce_str, //随机串
            'package' => 'prepay_id=' . $return['prepay_id'], //数据包
            'signType' => 'MD5'//签名方式
        );

        //签名
        $parameter['paySign'] = getSign($parameter, $key);
        $this->success('支付信息',$parameter);

    }

    /**
     * 全都订单
     */

    public function getallorder(){


        $user=$this->auth->getUser();


        $map['user_id']=['eq',$user['id']];


        $status=$this->request->param('status');
        if ($status){
            $map['status']=['eq',$status];
        }



        $list=Db::name('aa_service')
            ->where($map)
            ->order('id','desc')->select();

        foreach ($list as &$key){

            $key['time']=date('Y-m-d H:i',$key['time']);
            $good=Db::name('backgood')->where(['id'=>$key['backgood_id']])->find();
            $key['g_name']=$good['name'];
        }


        $this->success('展示订单',$list);

    }




    public function getordreinfobyid(){


        $id=$this->request->param('id');
        $list=Db::name('aa_service')
            ->where('id','eq',$id)
            ->find();


        $list['time']=date('Y-m-d H:i',$list['time']);
        $good=Db::name('backgood')->where(['id'=>$list['backgood_id']])->find();
        $list['g_name']=$good['name'];

        $this->success('订单详情',$list);

    }


    /**
     * 修改订单信息
     */
    public function express(){
        $id=$this->request->param('id');
        $express=$this->request->param('express');


       $info=Db::name('aa_service')
            ->where('id','eq',$id)
            ->find();

       if ($info['status']<1){
           $this->error('无法修改!');
       }


        Db::name('aa_service')
            ->where('id','eq',$id)
            ->update(['express1'=>$express,'status'=>2]);


        $this->success('成功');
    }


}